package com.zam.web.sysController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zam.comment.Result;
import com.zam.pojo.Menu;
import com.zam.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/******************************
 controller层
 ******************************/
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    /*
    添加数据
    */
    @PostMapping
    public Result save(@RequestBody Menu menu) {
        menuService.saveOrUpdate(menu);
        return Result.success();
    }

    /*
    删除数据
    */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        menuService.removeById(id);
        return Result.success();
    }

    /*
    删除多个数据
    */
    @DeleteMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        menuService.removeByIds(ids);
        return Result.success();
    }

    /*
     查看全部
    */
    @GetMapping
    public Result findAll(@RequestParam String name) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Menu::getName, name);
        List<Menu> list = menuService.list(queryWrapper);
        List<Menu> collect = list.stream().filter(m -> m.getPid() == null).collect(Collectors.toList());
        for (Menu menu : collect) {
            menu.setChildren(list.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        }
        return Result.success(collect);
    }

    /*
    根据id查看
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(menuService.getById(id));
    }

}

